package com.fz.us.admin.base.utils;

import com.google.common.collect.Lists;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * Created by ZhuChunXiao on 2017/4/25.
 */
public class EntityConfig {
    private String file;
    private static Properties pr;
    public EntityConfig(){
        this.file = "/config/entity.config";
    }
    public EntityConfig(String fileName){
        this.file = "/config/"+ fileName;
    }
    /**
     * 根据键来获得值
     * @param key
     * @return
     */
    public  String getValues(String key){

       // String fileName = this.getClass().getResource(file).getPath();
        try {
            if(pr == null){
                pr = new Properties();
            }
          //  pr.load(new FileInputStream(fileName));
            pr.load(new InputStreamReader( this.getClass().getResourceAsStream(file), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pr.getProperty(key);

    }

    /**
     * 获得全部属性
     * @return
     */
    public List<Map<String,Object>> getAllProperties(){
       // String fileName = this.getClass().getResource(file).getPath();
        try {
            if(pr == null){
                pr = new Properties();
            }
            pr.load(new InputStreamReader( this.getClass().getResourceAsStream(file), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration en = pr.propertyNames();
        List<Map<String,Object>> maps = Lists.newArrayList();
        Map<String,Object> rMap;
        while(en.hasMoreElements()) {
            rMap = new HashMap<String, Object>();
            String strKey = (String) en.nextElement();
            String strValue = pr.getProperty(strKey);
            rMap.put("key", strKey);
            rMap.put("value", strValue);
            maps.add(rMap);
        }
        LogUtil.info("mapssize:"+maps.size());
        return  maps;
    }
}
